package com.liveneo.adapter.mobile.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.liveneo.adapter.base.entity.PrpmSurveyUser;
import com.liveneo.adapter.base.entity.PrpmUserOnline;
import com.liveneo.adapter.base.mapper.PrpmUserOnlineMapper;
import com.liveneo.adapter.mobile.model.BaseRequest;
import com.liveneo.adapter.mobile.model.dto.CallBackUserListDto;
import com.liveneo.adapter.mobile.model.request.CallbackRequest;
import com.liveneo.adapter.mobile.service.PrpmSurveyUserService;
import com.liveneo.adapter.mobile.service.PrpmUserOnlineService;
import com.liveneo.adapter.mobile.util.JedisUtils;
import com.liveneo.adapter.mobile.util.PropertyConfigurer;
import com.liveneo.adapter.mobile.util.UUIDUtils;
import com.liveneo.system.base.utils.DateUtils;
import com.liveneo.system.base.utils.JsonUtils;

/**
 * @Desc：用户状态service实现
 * @Title：PrpmUserOnlineServiceImpl
 * @address: http://www.liveneo.com.cn/
 * @author: zhaoyy
 * @version: 1.0 
 * @date: 2016年11月23日 下午2:30:00
 */
@Service
public class PrpmUserOnlineServiceImpl implements PrpmUserOnlineService {
    public static String          rediskeyprefix = PropertyConfigurer.getValue("rediskeyprefix");
    @Autowired
    private PrpmUserOnlineMapper  userOnlineMapper;
    @Autowired
    private PrpmSurveyUserService surveyUserService;

    @Override
    public PrpmUserOnline selectByUserCodeAndUserType(String userCode, String userType) {
        Map<String, Object> map = new HashMap<String, Object>();
        if ("B".equals(userType)) {
            userType = "2";
        } else if ("C".equals(userType)) {
            userType = "3";
        } else if ("A".equals(userType)) {
            userType = "1";
        }
        map.put("userCode", userCode);
        map.put("userType", userType);
        return userOnlineMapper.selectByUserCodeAndUserType(map);
    }

    @Override
    public int updateUserOnline(PrpmUserOnline userOnline, BaseRequest request) {
        userOnline.setStatus(0);    // 0-空闲 1-繁忙 2-离开（暂未使用）
        userOnline.setOnlineTime(new Date());
        userOnline.setModifyTime(new Date());
        if ("B".equals(request.getProjectCode()) || "A".equals(request.getProjectCode())) {
            PrpmSurveyUser surveyUser = surveyUserService.findUserByUserCode(request.getUserId());
            if (surveyUser != null) {
                userOnline.setModifyBy(surveyUser.getSurveyName());
            }
        } else {
            userOnline.setModifyBy(request.getUserId());
        }
        return userOnlineMapper.updateByPrimaryKeySelective(userOnline);
    }

    @Override
    public int saveUserOnline(BaseRequest request) {
        PrpmUserOnline userOnline = new PrpmUserOnline();
        userOnline.setUserOnlineId(UUIDUtils.getUUIDStr());
        userOnline.setUserCode(request.getUserId());
        if ("B".equals(request.getProjectCode()) || "A".equals(request.getProjectCode())) {
            PrpmSurveyUser surveyUser = surveyUserService.findUserByUserCode(request.getUserId());
            if (surveyUser != null) {
                userOnline.setLoginName(surveyUser.getSurveyName());
                userOnline.setCreateBy(surveyUser.getSurveyName());
                userOnline.setOrgId(surveyUser.getOrgId());
                userOnline.setOrgCode(surveyUser.getOrgCode());
            }
        } else {
            userOnline.setCreateBy(request.getUserId());
            userOnline.setLoginName(request.getUserId());
        }
        userOnline.setStatus(0);    // 0-空闲 1-繁忙 2-离开（暂未使用）
        userOnline.setUserId(request.getUserId());
        userOnline.setOnlineTime(new Date());
        if ("B".equals(request.getProjectCode())) {
            userOnline.setUserType(2);
        } else if ("C".equals(request.getProjectCode())) {
            userOnline.setUserType(3);
        } else if ("A".equals(request.getProjectCode())) {
            userOnline.setUserType(1);
        }
        userOnline.setCreateTime(new Date());
        return userOnlineMapper.insertSelective(userOnline);
    }

    @Override
    public String savaCallbackRedis(CallbackRequest request) {
        String key = rediskeyprefix + request.getUserId();
        CallBackUserListDto dto = new CallBackUserListDto();
        dto.setProjectCode(request.getProjectCode());
        dto.setOnlineTime(DateUtils.formatDateTime(new Date()));
        dto.setUserId(request.getUserId());
        dto.setTerminalId(request.getTerminalId());
        return JedisUtils.set(key, JsonUtils.beanToJson(dto), 0);
    }
}
